package com.bw.zh.view

import android.os.Bundle
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.widget.ProgressBar
import androidx.activity.ComponentActivity
import com.alipay.sdk.app.PayTask
import com.bw.zh.R


/**
 *
 * @ClassName: WebViewActivity
 * @Description: java类作用描述
 * @Author: zhangyanchao
 * @CreateDate: 2024/9/28 10:54 AM
 * @Version: 1.0
 */
class WebViewActivity : ComponentActivity() {
    lateinit var webView: WebView
    lateinit var pb: ProgressBar
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_webview)
        webView = findViewById<WebView>(R.id.webview)
        pb = findViewById<ProgressBar>(R.id.pb)

        //加载网页地址
        webView.loadUrl("https://m.qfang.com/shenzhen/user/user-protocol.html")

        webView.webChromeClient = object : WebChromeClient() {
            override fun onProgressChanged(view: WebView, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                if (newProgress < 100) {
                    pb.progress = newProgress
                } else {
//                    hideLoadProgress()
                    pb.visibility = View.GONE
                }
            }

            override fun onReceivedTitle(view: WebView, title: String) {
                super.onReceivedTitle(view, title)
//                if (callBack != null) {
//                    callBack.onChange(title)
//                }
            }
        }
    }


    fun toPay(){
        val orderInfo: String = "app_id=2015052600090779&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%22%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.02%22%2C%22subject%22%3A%221%22%2C%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22314VYGIAGG7ZOYY%22%7D&charset=utf-8&method=alipay.trade.app.pay&sign_type=RSA2&timestamp=2016-08-15%2012%3A12%3A15&version=1.0&sign=MsbylYkCzlfYLy9PeRwUUIg9nZPeN9SfXPNavUCroGKR5Kqvx0nEnd3eRmKxJuthNUx4ERCXe552EV9PfwexqW%2B1wbKOdYtDIb4%2B7PL3Pc94RZL0zKaWcaY3tSL89%2FuAVUsQuFqEJdhIukuKygrXucvejOUgTCfoUdwTi7z%2BZzQ%3D" // 订单信息
        val payRunnable = Runnable {
            val alipay = PayTask(this@WebViewActivity)
            val result = alipay.payV2(orderInfo, true)

            if(result!=null){
                //跳转到支付结果页面
                //商品名称，商品价格，数量，支付金额，账号余额 ，支付成功
            }
        }
        // 必须异步调用
        val payThread = Thread(payRunnable)
        payThread.start()
    }
}